<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>前端基础总结 | 欢迎来到小羽的博客</title>
    <meta name="generator" content="VuePress 1.5.0">
    <link rel="icon" href="/favicon.ico">
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b61fe05f9bcd9cda05e839091f9a55a4";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
    <script type="text/javascript" src="/push.js"></script>
    <script type="text/javascript" src="/bdseo.js"></script>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <link rel="preload" href="/assets/css/0.styles.535c5086.css" as="style"><link rel="preload" href="/assets/js/app.14e3b6f2.js" as="script"><link rel="preload" href="/assets/js/3.38ef4e4c.js" as="script"><link rel="preload" href="/assets/js/1.dd4aade8.js" as="script"><link rel="preload" href="/assets/js/12.b0df0a98.js" as="script"><link rel="prefetch" href="/assets/js/10.7327de6d.js"><link rel="prefetch" href="/assets/js/11.dbb0142d.js"><link rel="prefetch" href="/assets/js/13.478ebb6f.js"><link rel="prefetch" href="/assets/js/14.5b94084a.js"><link rel="prefetch" href="/assets/js/15.159d1a32.js"><link rel="prefetch" href="/assets/js/16.cf37c681.js"><link rel="prefetch" href="/assets/js/17.61f89c90.js"><link rel="prefetch" href="/assets/js/18.52aae943.js"><link rel="prefetch" href="/assets/js/19.152fd37e.js"><link rel="prefetch" href="/assets/js/20.ce91f28e.js"><link rel="prefetch" href="/assets/js/21.47bc5b36.js"><link rel="prefetch" href="/assets/js/22.4bb2e24d.js"><link rel="prefetch" href="/assets/js/23.8156f657.js"><link rel="prefetch" href="/assets/js/24.867f6f46.js"><link rel="prefetch" href="/assets/js/25.1195288f.js"><link rel="prefetch" href="/assets/js/26.87eb9ee8.js"><link rel="prefetch" href="/assets/js/27.f67176ce.js"><link rel="prefetch" href="/assets/js/28.9574af6a.js"><link rel="prefetch" href="/assets/js/29.ce6e5d4c.js"><link rel="prefetch" href="/assets/js/30.5a16f9ff.js"><link rel="prefetch" href="/assets/js/31.d9981107.js"><link rel="prefetch" href="/assets/js/32.e0da40e8.js"><link rel="prefetch" href="/assets/js/33.f50cc0ef.js"><link rel="prefetch" href="/assets/js/34.6d343a0b.js"><link rel="prefetch" href="/assets/js/35.6e3a0c31.js"><link rel="prefetch" href="/assets/js/36.38b9af99.js"><link rel="prefetch" href="/assets/js/37.87aede0d.js"><link rel="prefetch" href="/assets/js/4.6cf77dfa.js"><link rel="prefetch" href="/assets/js/5.c81d3913.js"><link rel="prefetch" href="/assets/js/6.8fadf7af.js"><link rel="prefetch" href="/assets/js/7.bb84db9a.js"><link rel="prefetch" href="/assets/js/8.d9edba17.js"><link rel="prefetch" href="/assets/js/9.c228f55c.js">
    <link rel="stylesheet" href="/assets/css/0.styles.535c5086.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-dad8a512><div data-v-dad8a512><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-dad8a512 data-v-dad8a512><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-64685f0e data-v-dad8a512 data-v-dad8a512><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>欢迎来到小羽的博客</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>小羽</span>
            
          <!---->
          2020
        </a></span></div></div> <div class="hide" data-v-dad8a512><header class="navbar" data-v-dad8a512><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="欢迎来到小羽的博客" class="logo"> <span class="site-name">欢迎来到小羽的博客</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/安全/" class="nav-link"><i class="iconfont undefined"></i>
  安全
</a></li><li class="dropdown-item"><!----> <a href="/categories/网络/" class="nav-link"><i class="iconfont undefined"></i>
  网络
</a></li><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="iconfont undefined"></i>
  运维
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      其他
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lyff1006" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/xiaoyu-web" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/fly821760648" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-csdn"></i>
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://juejin.im/user/3597257778926973" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-juejin"></i>
  掘金
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://www.jianshu.com/u/4ab50cbafc3f" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-jianshu"></i>
  简书
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-dad8a512></div> <aside class="sidebar" data-v-dad8a512><div class="personal-info-wrapper" data-v-ca798c94 data-v-dad8a512><img src="/logo.png" alt="author-avatar" class="personal-img" data-v-ca798c94> <h3 class="name" data-v-ca798c94>
    小羽
  </h3> <div class="num" data-v-ca798c94><div data-v-ca798c94><h3 data-v-ca798c94>22</h3> <h6 data-v-ca798c94>文章</h6></div> <div data-v-ca798c94><h3 data-v-ca798c94>35</h3> <h6 data-v-ca798c94>标签</h6></div></div> <hr data-v-ca798c94></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/安全/" class="nav-link"><i class="iconfont undefined"></i>
  安全
</a></li><li class="dropdown-item"><!----> <a href="/categories/网络/" class="nav-link"><i class="iconfont undefined"></i>
  网络
</a></li><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="iconfont undefined"></i>
  运维
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      其他
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lyff1006" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/xiaoyu-web" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/fly821760648" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-csdn"></i>
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://juejin.im/user/3597257778926973" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-juejin"></i>
  掘金
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://www.jianshu.com/u/4ab50cbafc3f" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-jianshu"></i>
  简书
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-64685f0e data-v-dad8a512><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>前端基础总结</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>小羽</span>
            
          <!---->
          2020
        </a></span></div></div> <div data-v-dad8a512><main class="page"><div class="page-title" style="display:none;"><h1>前端基础总结</h1> <hr> <div data-v-aa64b9d0><i class="iconfont reco-account" data-v-aa64b9d0><span data-v-aa64b9d0>小羽</span></i> <i class="iconfont reco-date" data-v-aa64b9d0><span data-v-aa64b9d0>2020-08-16</span></i> <!----> <i class="iconfont reco-tag tags" data-v-aa64b9d0><span class="tag-item" data-v-aa64b9d0>前端基础</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h2 id="_1-数据类型"><a href="#_1-数据类型" class="header-anchor">#</a> 1.数据类型</h2> <h3 id="_1-1-数据类型分类"><a href="#_1-1-数据类型分类" class="header-anchor">#</a> 1.1 数据类型分类</h3> <p>最新的ECMAScript（es10）标准定义了8种数据类型</p> <ul><li>7种原始类型
<ul><li>Boolean</li> <li>Undefined</li> <li>Number</li> <li>Null</li> <li>String</li> <li>Symbol（es6）</li> <li>BigInt（es10）</li></ul></li> <li>引用类型
<ul><li>Object</li> <li>Array</li> <li>Date</li> <li>RegExp</li> <li>Function</li> <li>基本包装类型（Boolean、Number、String）</li></ul></li></ul> <blockquote><p>原始类型和引用类型的区别：</p> <p>1.原始数据类型是存储在栈内存种的，引用数据类型是存储在堆内存中的。</p> <p>2.js在创建一个变量的时候，如果是原始类型，那js引擎会直接在内存的栈中初始化一个原始类型并返回；如果是对象类型，那么js引擎会先在内存堆中初始化并创建一个对象，然后在栈中存储这个对象的指针。所以对于引用类型，变量存储的是一个指针，原始类型存储的是值。</p></blockquote> <h3 id="_1-2-类型判断"><a href="#_1-2-类型判断" class="header-anchor">#</a> 1.2 类型判断</h3> <ul><li><code>typeof</code>：得到的值有以下几种类型：<code>undefined</code>、<code>boolean</code>、<code>number</code>、<code>string</code>、<code>object</code>、<code>function</code>、<code>symbol</code> ，对于原始数据类型，要注意<code>typeof null = 'object'</code>。对于引用数据类型的判断，除<code>typeof function</code>的值为<code>function</code>，其余均为<code>objcet</code></li> <li><code>instanceof</code>：用于实例和构造函数的对应。例如判断一个变量是否是数组，使用typeof无法判断，但可以使用<code>[1, 2] instanceof Array</code>来判断。因为，<code>[1, 2]</code>是数组，它的构造函数就是<code>Array</code>。</li></ul> <h2 id="_2-作用域"><a href="#_2-作用域" class="header-anchor">#</a> 2.作用域</h2> <h3 id="_2-1-执行上下文"><a href="#_2-1-执行上下文" class="header-anchor">#</a> 2.1 执行上下文</h3> <p>JS在编译一段代码的过程中，会为其生成一个对应的执行环境，这个执行环境就是<code>执行上下文</code>，在<code>执行上下文</code>环境中，保存着代码块的<code>变量环境</code>、<code>词法环境</code>、<code>外层作用域链指针</code>，以及<code>this</code>指针等。</p> <p>JS的执行上下文环境分为以下三种：</p> <ol><li><code>全局执行上下文</code>：当 JavaScript 执行全局代码的时候，会编译全局代码并创建全局执行上下文，而且在 整个页面的生存周期内，全局执行上下文只有一份。</li> <li><code>函数执行上下文</code>：当调用一个函数的时候，函数体内的代码会被编译，并创建函数执行上下文，一般情况 下，函数执行结束之后，创建的函数执行上下文会被销毁。</li> <li><code>eval执行上下文</code>：当使用<code>eval</code>函数的时候，·eval·的代码也会被编译，并创建执行上下文。</li></ol> <p>执行上下文是JS代码编译的产物之一，里面存放了代码块里的各种变量，后续对代码执行时对变量的各种操作，都是基于这个执行上下文环境的。</p> <h3 id="_2-2-调用栈"><a href="#_2-2-调用栈" class="header-anchor">#</a> 2.2 调用栈</h3> <p>JavaScript 引擎正是利用栈来管理执行上下文的。在执行上下文创建好后，JavaScript引擎会将执行上下文压入栈中，通常把这种用来管理执行上下文的栈称为执行上下文栈，又称调用栈。</p> <p>执行上下文主要有<code>全局执行上下文</code>与<code>函数执行上下文</code>，JS在执行一段代码的时候，最开始会初始化一个全局执行上下文，并将这个全局执行上下文压到调用栈中，当执行到某个函数时，又会为这个函数创建执行上下文，并将函数的执行上下文入栈，如果函数里面调用了其他函数，则会继续为调用的函数创建新的执行上下文并且入栈，函数执行完后，函数执行上下文出栈。</p> <h3 id="_2-3-变量提升"><a href="#_2-3-变量提升" class="header-anchor">#</a> 2.3 变量提升</h3> <p>所谓的<code>变量提升</code>，是指在JS代码执行过程中，JS引擎把变量的声明部分和函数的声明部分提升到代码开头的“行为”。</p> <p>不过这也只是一个比较形象的解释，实质上变量和函数声明在代码里的位置是不会改变的，而是引擎在编译阶段将变量与函数的声明放入当前代码块<code>执行上下文</code>环境的<code>变量环境</code>中去，并且对于变量会默认设置为<code>undefined</code>。在代码执行阶段，JS引擎会从变量环境中去查找自定义的变量和函数。</p> <p>具体有以下几点需要注意：</p> <ol><li>如果存在两个同名函数，后者声明会覆盖前者的声明</li> <li>如果变量与函数同名，那么JS引擎会忽略变量声明</li></ol> <p>此外，<code>变量提升</code>存在变量覆盖、变量污染等设计缺陷</p> <h3 id="_2-4-作用域"><a href="#_2-4-作用域" class="header-anchor">#</a> 2.4 作用域</h3> <p>作用域就是变量和函数的可使用范围，在ES6之前，JS的作用域只有两种：全局作用域和函数作用域。</p> <ul><li><code>全局作用域</code>：中的对象在代码中的任何地方都能访问，其生命周期伴随着页面的生命周期。</li> <li><code>函数作用域</code>：就是在函数内部定义的变量或者函数，并且定义的变量或者函数只能在函数内部被访问。函数执行结束之后，函数内部定义的变量会被销毁。</li></ul> <p><code>es6</code>通过引入<code>let</code>与<code>const</code>关键字支持了块级作用域，用<code>let</code>或<code>const</code>声明的变量存放在<code>当前执行上下文</code>环境的<code>词法环境</code>中。</p> <p>在词法环境内部，维护了一个小型栈结构，栈底是函数最外层通过<code>let</code>或者<code>const</code>声明的变量，进入一个作用域块后，就会把该作用域块内部通过<code>let</code>或者<code>const</code>声明的变量压到栈顶；当作用域执行完成之后，该作用域的信息就会从栈顶弹出，这就是词法环境的结构。</p> <p>当JS需要查询某个变量时，会先在词法环境这个变量栈中从栈顶往栈底一层层查找，查到了就直接返回，如果都查不到，再会到变量环境中去找。这就是JS变量在当前执行上下文内变量查找的步骤。</p> <h3 id="_2-5-作用域链"><a href="#_2-5-作用域链" class="header-anchor">#</a> 2.5 作用域链</h3> <p>每个执行上下文环境中，都保存着一个称为<code>outer</code>外部引用，来指向外部的上下执行文环境。当一段代码使用了某个变量的时候，JS引擎会先在当前的<code>执行上下文</code>环境中查询，查到就返回，查不到就会沿着<code>outer</code>指向的上下文环境继续查找。这个通过<code>outer</code>串起来的查询链我们就称之为<code>作用域链</code>。</p> <p>JS的<code>词法作用域</code>是<code>作用域链</code>的基础，<code>词法作用域</code>是在代码编译阶段就确定的，同样的<code>作用域链</code>在代码编译阶段就确定了，与函数的执行阶段如何调用无关。</p> <h2 id="_3-闭包"><a href="#_3-闭包" class="header-anchor">#</a> 3.闭包</h2> <h3 id="_3-1-概念"><a href="#_3-1-概念" class="header-anchor">#</a> 3.1 概念</h3> <p>闭包函数：声明在一个函数中的函数叫闭包函数。</p> <p>闭包： 内部函数总是可以访问其所在的外部函数中声明的参数和变量，即使在其外部函数被返回（生命周期结束）之后。</p> <h3 id="_3-2-特点"><a href="#_3-2-特点" class="header-anchor">#</a> 3.2 特点</h3> <ul><li>让外部访问函数内部变量成为可能</li> <li>局部变量会常驻于内存中</li> <li>可以避免使用全局变量，防止全局变量污染</li> <li>滥用闭包会造成内存泄漏（有一块内存空间被长期占用，而不被释放）</li></ul> <h3 id="_3-3-闭包的创建"><a href="#_3-3-闭包的创建" class="header-anchor">#</a> 3.3 闭包的创建</h3> <p>闭包就是可以创建一个独立的环境，每个闭包里面的环境都是独立的，互不干扰。闭包会发生内存泄漏，每次外部函数执行的时 候，外部函数的引用地址不同，都会重新创建一个新的地址。但凡是当前活动对象中有被内部子集引用的数据，那么这个时候，这个数据不删除，保留一根指针给内部活动对象。</p> <h3 id="_3-4-闭包的应用场景"><a href="#_3-4-闭包的应用场景" class="header-anchor">#</a> 3.4 闭包的应用场景</h3> <ul><li>模拟块级作用域</li> <li>柯里化函数</li> <li>防抖和节流</li></ul> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">//模拟块级作用域</span>
<span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;=</span><span class="token number">3</span> <span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">alert</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h2 id="_4-this"><a href="#_4-this" class="header-anchor">#</a> 4.this</h2> <h3 id="_4-1-this的指向"><a href="#_4-1-this的指向" class="header-anchor">#</a> 4.1 this的指向</h3> <p><code>this</code>是和执行上下文绑定的,每个执行上下文中都有一个<code>this</code>,this指向与运行时的调用状态相关。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>a<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token number">1</span>
<span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token keyword">const</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
  a<span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
  foo<span class="token operator">:</span> foo
<span class="token punctuation">}</span>
obj<span class="token punctuation">.</span><span class="token function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> c <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><ol><li>对于直接调用 <code>foo</code> 来说，不管 <code>foo</code> 函数被放在了什么地方，<code>this</code> 一定是 <code>window</code></li> <li>对于 <code>obj.foo()</code> 来说，我们只需要记住，谁调用了函数，谁就是 <code>this</code>，所以在这个场景下 <code>foo</code> 函数中的 <code>this</code>就是 obj 对象</li> <li>对于 <code>new</code> 的方式来说，<code>this</code> 被永远绑定在了 <code>c</code> 上面，不会被任何方式改变 <code>this</code></li> <li>箭头函数是没有<code>this</code>的，箭头函数中的 <code>this</code> 只取决包裹箭头函数的第一个普通函数的 <code>this</code>。</li></ol> <h3 id="_4-2-改变this的指向"><a href="#_4-2-改变this的指向" class="header-anchor">#</a> 4.2 改变this的指向</h3> <p>可以通过调用函数的<code>call()</code>、<code>apply()</code>、<code>bind()</code> 来改变<code>this</code>的指向。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
    name<span class="token operator">:</span><span class="token string">'小鹿'</span><span class="token punctuation">,</span>
    age<span class="token operator">:</span><span class="token string">'22'</span><span class="token punctuation">,</span>
    adress<span class="token operator">:</span><span class="token string">'小鹿动画学编程'</span>
<span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment">// 打印 this 的指向</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// 打印传递的参数</span>
<span class="token punctuation">}</span>
<span class="token comment">// 通过 call 改变 this 指向</span>
<span class="token function">print</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   
<span class="token comment">// 通过 apply 改变 this 指向</span>
<span class="token function">print</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 通过 bind 改变 this 的指向</span>
<span class="token keyword">let</span> fn <span class="token operator">=</span> <span class="token function">print</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><ul><li><code>共同点</code>：三者都能改变<code>this</code>指向，且第一个传递的参数都是<code>this</code>指向的对象，三者都采用的后续传参的形式。</li> <li><code>不同点</code>：<code>call</code>的传参是单个传递的（试了下数组，也是可以的），而<code>apply</code>后续传递的参数是数组形式（传单个值会报错），而 <code>bind</code>没有规定，传递值和数组都可以。<code>call</code>和<code>apply</code>函数的执行是直接执行的，而<code>bind</code>函数会返回一个函数，然后我们想要调用的时候才会执行。</li></ul> <p>关于this有几点需注意：</p> <ol><li>如果多次调用bind改变this指向，只有第一次会起作用。</li> <li>如果同时出现多种this规则，这时候不同的规则之间会根据优先级最高的来决定 this 最终指向哪里。具体优先级为： <code>new</code> &gt; <code>call(),apply(),bind()</code> &gt; <code>obj.foo()</code> &gt; <code>foo()</code></li> <li>箭头函数this一旦被绑定就不会再改变。 由于箭头函数没有自己的<code>this</code>指针，通过<code>call()</code>或<code>apply()</code>方法调用一个函数时，只能传递参数（不能绑定<code>this</code>），他们的第一个参数会被忽略。</li></ol> <h2 id="_5-string-和-array-常用函数"><a href="#_5-string-和-array-常用函数" class="header-anchor">#</a> 5.String 和 Array 常用函数</h2> <h3 id="_5-1-string"><a href="#_5-1-string" class="header-anchor">#</a> 5.1 String</h3> <ul><li>split(): 使用指定的分隔符字符串将一个String对象分割成子字符串数组</li> <li>slice(): 提取某个字符串的一部分，并返回一个新的字符串，且不会改动原字符串</li> <li>substring(): 返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集</li></ul> <h3 id="_5-2-array"><a href="#_5-2-array" class="header-anchor">#</a> 5.2 Array</h3> <ul><li>slice(): 方法返回一个新的数组对象，这一对象是一个由begin 和end决定的原数组的浅拷贝（包括begin，不包括end）。原始数组不会被改变。</li> <li>splice(): 方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。</li> <li>push(): 方法将一个或多个元素添加到数组的末尾，并返回该数组的新长度。</li> <li>pop(): 方法从数组中删除最后一个元素，并返回该元素的值。此方法更改数组的长度。</li> <li>shift():方法从数组中删除第一个元素，并返回该元素的值。此方法更改数组的长度。</li> <li>unshift(): 方法将一个或多个元素添加到数组的开头，并返回该数组的新长度(该方法修改原有数组)。</li></ul> <h2 id="_6-堆内存和栈内存"><a href="#_6-堆内存和栈内存" class="header-anchor">#</a> 6. 堆内存和栈内存</h2> <p>在js引擎中对变量的存储主要有两种位置，堆内存和栈内存。
栈内存主要用于存储各种基本类型的变量，包括Boolean、Number、String、Undefined、Null。栈内存中的变量一般都是已知大小或者有范围上限的，算作一种简单存储。</p> <p>而堆内存主要负责像对象Object、Array这种引用数据类型的存储，对于存储的数据大小这方面，一般都是未知的。</p> <p>栈内存是直接存取数据的，而堆内存是指向存取数据所在的地址，这点很重用！！！</p> <h2 id="_7-浅拷贝和深拷贝"><a href="#_7-浅拷贝和深拷贝" class="header-anchor">#</a> 7.浅拷贝和深拷贝</h2> <p>浅拷贝和深拷贝这两个概念主要是针对引用数据类型的。上面我们有说过引用数据类型是存取数据所在的地址，而平常我们修改数组、对象的时候是直接修改对应地址里面的数据。当我们让一个新的数据等于一个旧的数据如var newarr = oldarr，这时候我们直接对newarr进行修改时，就会直接修改newarr对应的内存地址里面的数据。这时候，我们可以发现oldarr也被更改了并且数据与newarr一样。为了不让这种情况出现，于是就有了与之对应的深拷贝。
深拷贝简单来说就是重新开辟一个新的内存空间，然后让我们的对象、数组指向这个新的内存空间，然后我们对这个新的内存空间进行修改时是不会影响到旧的数组、对象。因为他们指向了不同的内存空间</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>	<span class="token comment">/**
     * @description: 判断数据类型
     * @Date: 2020-04-30 00:42:17
     * @author: 小羽
     * @param {type} 
     * @return: 数据类型（如boolean，number，string等）
     */</span>
    <span class="token function">getType</span><span class="token punctuation">(</span><span class="token parameter">obj</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> map <span class="token operator">=</span> <span class="token punctuation">{</span>
            <span class="token string">'[object Boolean]'</span><span class="token operator">:</span> <span class="token string">'boolean'</span><span class="token punctuation">,</span>
            <span class="token string">'[object Number]'</span><span class="token operator">:</span> <span class="token string">'number'</span><span class="token punctuation">,</span>
            <span class="token string">'[object String]'</span><span class="token operator">:</span> <span class="token string">'string'</span><span class="token punctuation">,</span>
            <span class="token string">'[object Function]'</span><span class="token operator">:</span> <span class="token string">'function'</span><span class="token punctuation">,</span>
            <span class="token string">'[object Array]'</span><span class="token operator">:</span> <span class="token string">'array'</span><span class="token punctuation">,</span>
            <span class="token string">'[object Date]'</span><span class="token operator">:</span> <span class="token string">'date'</span><span class="token punctuation">,</span>
            <span class="token string">'[object RegExp]'</span><span class="token operator">:</span> <span class="token string">'regExp'</span><span class="token punctuation">,</span>
            <span class="token string">'[object Undefined]'</span><span class="token operator">:</span> <span class="token string">'undefined'</span><span class="token punctuation">,</span>
            <span class="token string">'[object Null]'</span><span class="token operator">:</span> <span class="token string">'null'</span><span class="token punctuation">,</span>
            <span class="token string">'[object Object]'</span><span class="token operator">:</span> <span class="token string">'object'</span>
        <span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> map<span class="token punctuation">[</span><span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

	<span class="token comment">/**
     * @description: 深拷贝
     * @Date: 2020-04-30 00:44:01
     * @author: 小羽
     * @param {type} 
     * @return: 进行深拷贝后的data
     */</span>
    <span class="token function">deepClone</span><span class="token punctuation">(</span><span class="token parameter">data</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">let</span> type <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">let</span> obj<span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>type <span class="token operator">===</span> <span class="token string">'array'</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            obj <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>type <span class="token operator">===</span> <span class="token string">'object'</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token comment">//不再具有下一层次</span>
            <span class="token keyword">return</span> data<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>type <span class="token operator">===</span> <span class="token string">'array'</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> len <span class="token operator">=</span> data<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                obj<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">deepClone</span><span class="token punctuation">(</span>data<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>type <span class="token operator">===</span> <span class="token string">'object'</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> key <span class="token keyword">in</span> data<span class="token punctuation">)</span><span class="token punctuation">{</span>
                obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">deepClone</span><span class="token punctuation">(</span>data<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> obj<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br></div></div><h2 id="_8-原型和原型链"><a href="#_8-原型和原型链" class="header-anchor">#</a> 8.原型和原型链</h2> <p>见名知意，所谓的&quot;<strong>链</strong>&quot;描述的其实是一种关系，加上<strong>原型</strong>两个字，可以理解为原型之间的关系，既然是一种关系，就需要维系，就好比我们走亲访友，亲情就是一种纽带，类比在JavaScript当中——<strong>函数、对象实例、实例原型</strong>
也有自身的联系，而他们之间的纽带就是下面这三个重要的属性：</p> <p><strong>三个重要的属性：<code>prototype</code>、<code>__proto__</code>、<code>constructor</code></strong></p> <h3 id="_8-1-prototype"><a href="#_8-1-prototype" class="header-anchor">#</a> 8.1 prototype</h3> <p>所谓属性，指的是一个事物的特征，就比如美女的一大特征是“大长腿”，那“大长腿&quot;就是美女的属性，类比到JavaScript中函数，每一个函数都有一个<code>prototype</code>属性，这属性就是与生俱来的特质。这里需要特别强调一下，是<strong>函数</strong>，普通的对象是没有这个属性的，（这里为什么说普通对象呢，因为在JavaScript里面，<strong>一切皆为对象</strong>，所以这里的普通对象不包括函数对象）</p> <p>我们来看一个例子：</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

<span class="token punctuation">}</span>
<span class="token comment">// 虽然写在注释里面，但是需要注意的是</span>
<span class="token comment">// prototype 是函数才会有的属性 （哈哈哈，看来在JavaScript中函数果然是有特权的……）</span>
<span class="token class-name">Person</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">&quot;Kevin&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> person1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> person2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>person1<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token comment">// Kevin</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>person2<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token comment">// Kevin</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>上面的代码中我们创建了一个构造函数<code>Person</code>，并且在实例原型上面添加了一个<code>name</code>属性赋值为<code>&quot;Kevin&quot;</code>;</p> <p>然后分别创建了两个实例对象:<code>person1、person2</code>;</p> <p>当我们打印两个实例对象上name属性时均输出了<code>Kevin</code>(可以亲自试一下)。</p> <p>我们不禁疑惑，这个<code>Person.prototype</code>到底是什么，为什么在上面添加属性，在
构造函数的实例化对象上都能访问到呢？</p> <p>其实 Person这个函数的<code>prototype</code>属性指向了一个对象,即:<code>Person.prototype</code>也是一个对象。（真是好多对象）<strong>这个对象正是调用该构造函数而创建的实例的原型</strong>。也就是这个例子中的<code>person1</code>和<code>person2</code>的原型。</p> <p>为了便于理解，我们将上面的这段话拆解一下：</p> <blockquote><ul><li>1.调用的构造函数：  Person</li> <li>2.使用什么调用：    new关键字</li> <li>3.得到了什么：     两个实例化对象person1、person2</li> <li>4.实例化对象和原型是什么关系: person1和person2的原型就是 Person.prototype</li></ul></blockquote> <p>那什么是原型呢？可以这样理解：每一个JavaScript对象（null除外）在创建的时候就会与之关联另外一个对象，这个对象就是我们所说的原型，而每一个对象都会从原型&quot;继承&quot;属性。</p> <p>上面的代码中我们并没有直接在<code>person1</code>和<code>person2</code>中添加name属性 但是这两个对象
却能够访问name属性,就是这个道理。</p> <h3 id="_8-2-proto"><a href="#_8-2-proto" class="header-anchor">#</a> 8.2 <code>__proto__</code></h3> <p>其实这是每一个JavaScript对象（除了null）都具有的一个属性，叫__proto__,这个属性会指向该对象的原型，即作为实例对象和实例原型的之间的链接桥梁,<strong>这里强调，是对象，同样，因为函数也是对象，所以函数也有这个属性。</strong></p> <p>我们看一个代码示例：</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

<span class="token punctuation">}</span>

<span class="token keyword">var</span> person <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>person<span class="token punctuation">.</span>__proto__ <span class="token operator">===</span> <span class="token class-name">Person</span><span class="token punctuation">.</span>prototype<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//true;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="_8-3-constructor"><a href="#_8-3-constructor" class="header-anchor">#</a> 8.3 constructor</h3> <p>实例原型指向实例的属性倒是没有,因为一个构造函数可能会生成很多个实例，但是原型指向构造函数的属性倒是有的，这就是我们的<code>constructor</code>——每一个原型都有一个<code>constructor</code>属性指向关联的构造函数。</p> <p>我们再来看一个示例：</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Person <span class="token operator">===</span> <span class="token class-name">Person</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>constructor<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>通过对三个属性的介绍，我们总结一下：</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

<span class="token punctuation">}</span>

<span class="token keyword">var</span> person <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>person<span class="token punctuation">.</span>__proto__ <span class="token operator">==</span> <span class="token class-name">Person</span><span class="token punctuation">.</span>prototype<span class="token punctuation">)</span> <span class="token comment">// true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token class-name">Person</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>constructor <span class="token operator">==</span> Person<span class="token punctuation">)</span> <span class="token comment">// true</span>
<span class="token comment">// 顺便学习一个ES5的方法,可以获得对象的原型</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Object<span class="token punctuation">.</span><span class="token function">getPrototypeOf</span><span class="token punctuation">(</span>person<span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token class-name">Person</span><span class="token punctuation">.</span>prototype<span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>上述代码中我们我们执行了以下操作：</p> <blockquote><ul><li>1.声明了构造函数 Person;</li> <li>2.使用new操作符调用 Person 实例化了一个person 对象;</li> <li>3.判断实例化对象通过__proto__是否指向实例原型;</li> <li>4.判断实例原型通过constructor是否能找到对应的构造函数;</li> <li>5.使用Object.getPrototypeOf方法传入一个对象 找到对应的原型对象;</li></ul></blockquote> <h3 id="_8-4-实例与原型"><a href="#_8-4-实例与原型" class="header-anchor">#</a> 8.4 实例与原型</h3> <p><strong>当读取实例的属性时，如果找不到，就会查找与对象关联的原型中的属性，如果还查不到，就去找原型的原型，一直找到最顶层为止。</strong></p> <p>我们再举一个例子：</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>  <span class="token keyword">function</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token punctuation">}</span>

  <span class="token class-name">Person</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'Kevin'</span><span class="token punctuation">;</span>

  <span class="token keyword">var</span> person <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  person<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'Daisy'</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>person<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token comment">// Daisy</span>

  <span class="token keyword">delete</span> person<span class="token punctuation">.</span>name<span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>person<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token comment">// Kevin</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>在上面这个例子中，我们给实例person添加了name 属性，当我们打印person.name的时候，结果自然为Daisy</p> <p>但是当我们删除了<code>person</code>下面的name属性后，读取<code>person.name</code>，依然能够成功输出Kevin，实际情况是从 person 对象中找不到 name 属性就会从 person 的原型也就是 <code>person.__proto__</code> ，也就是 <code>Person.prototype</code>中查找，幸运的是我们找到了 name 属性，结果为 Kevin。</p> <h3 id="_8-5-原型的原型"><a href="#_8-5-原型的原型" class="header-anchor">#</a> 8.5 原型的原型</h3> <p>我们前面提到过，原型也是一个对象，那么既然是对象，那肯定就有创建它的构造函数，
这个构造函数就是Object();</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>  <span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  obj<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'Kevin'</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>obj<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Kevin;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_8-6-原型链"><a href="#_8-6-原型链" class="header-anchor">#</a> 8.6 原型链</h3> <p>那Object.prototype 的原型呢？Object是根节点的对象，再往上查找就是null，我们可以打印：</p> <div class="language-JavaScript line-numbers-mode"><pre class="language-javascript"><code>  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token class-name">Object</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>__proto__ <span class="token operator">===</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>然而 null 究竟代表了什么呢？</p> <p>引用阮一峰老师的 《undefined与null的区别》 就是：</p> <p>null 表示“没有对象”，即该处不应该有值。</p> <p>所以 Object.prototype.<strong>proto</strong> 的值为 null 跟 Object.prototype 没有原型，其实表达了一个意思。</p> <p>所以查找属性的时候查到 Object.prototype 就可以停止查找了。</p> <p>我们可以将null 也加入最后的关系图中，这样就比较完整了。</p> <p>图中相互关联的原型组成的链状结构就是原型链，也就是红色的这条线</p> <p><img src="/assets/img/3323631-5c7112b10b75f_articlex.f3162d7d.png" alt="3323631-5c7112b10b75f_articlex"></p> <h2 id="_9-冒泡和捕获"><a href="#_9-冒泡和捕获" class="header-anchor">#</a> 9.冒泡和捕获</h2> <h3 id="_9-1-事件冒泡与事件捕获"><a href="#_9-1-事件冒泡与事件捕获" class="header-anchor">#</a> 9.1 事件冒泡与事件捕获</h3> <p>事件冒泡和事件捕获分别由微软和网景公司提出，这两个概念都是为了解决页面中<strong>事件流</strong>（事件发生顺序）的问题。</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>outer<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>inner<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Click me!<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>复制代码
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面的代码当中一个div元素当中有一个p子元素，如果两个元素都有一个click的处理函数，那么我们怎么才能知道哪一个函数会首先被触发呢？</p> <p>为了解决这个问题微软和网景提出了两种几乎完全相反的概念。</p> <h4 id="_9-1-1-事件冒泡"><a href="#_9-1-1-事件冒泡" class="header-anchor">#</a> 9.1.1 事件冒泡</h4> <p>微软提出了名为<strong>事件冒泡</strong>(event bubbling)的事件流。事件冒泡可以形象地比喻为把一颗石头投入水中，泡泡会一直从水底冒出水面。也就是说，事件会从最内层的元素开始发生，一直向上传播，直到document对象。</p> <p>因此上面的例子在事件冒泡的概念下发生click事件的顺序应该是</p> <p><strong>p -&gt; div -&gt; body -&gt; html -&gt; document</strong></p> <h4 id="_9-1-2-事件捕获"><a href="#_9-1-2-事件捕获" class="header-anchor">#</a> 9.1.2 事件捕获</h4> <p>网景提出另一种事件流名为<strong>事件捕获</strong>(event capturing)。与事件冒泡相反，事件会从最外层开始发生，直到最具体的元素。</p> <p>上面的例子在事件捕获的概念下发生click事件的顺序应该是</p> <p><strong>document -&gt; html -&gt; body -&gt; div -&gt; p</strong></p> <h3 id="_9-2-addeventlistener-的第三个参数"><a href="#_9-2-addeventlistener-的第三个参数" class="header-anchor">#</a> 9.2 addEventListener 的第三个参数</h3> <p>DOM2级事件”中规定的事件流同时支持了事件捕获阶段和事件冒泡阶段，而作为开发者，我们可以选择事件处理函数在哪一个阶段被调用。</p> <p>addEventListener方法用来为一个特定的元素绑定一个事件处理函数，是JavaScript中的常用方法。addEventListener有三个参数：</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code> element<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span>event<span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">,</span> useCapture<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><table><thead><tr><th>参数</th> <th>描述</th></tr></thead> <tbody><tr><td>event</td> <td>必须。字符串，指定事件名。 <br><strong>注意:</strong> 不要使用 &quot;on&quot; 前缀。 例如，使用 &quot;click&quot; ,而不是使用 &quot;onclick&quot;。   <br><strong>提示：</strong> 所有 HTML DOM 事件，可以查看我们完整的 <a href="http://www.runoob.com/jsref/dom-obj-event.html" target="_blank" rel="noopener noreferrer">HTML DOM Event 对象参考手册<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>。</td></tr> <tr><td>function</td> <td>必须。指定要事件触发时执行的函数。   <br>当事件对象会作为第一个参数传入函数。 事件对象的类型取决于特定的事件。例如， &quot;click&quot; 事件属于 MouseEvent(鼠标事件) 对象。</td></tr> <tr><td>useCapture</td> <td>可选。布尔值，指定事件是否在捕获或冒泡阶段执行。 <br>可能值:<br>true - 事件句柄在捕获阶段执行（即在事件捕获阶段调用处理函数）<br>false-- 默认。事件句柄在冒泡阶段执行（即表示在事件冒泡的阶段调用事件处理函数）</td></tr></tbody></table> <h3 id="_9-3-事件委托"><a href="#_9-3-事件委托" class="header-anchor">#</a> 9.3 事件委托</h3> <p>那什么叫事件委托呢？它还有一个名字叫事件代理，JavaScript高级程序设计上讲：事件委托就是利用事件冒泡，只指定一个事件处理程序，就可以管理某一类型的所有事件。</p> <p>例子：</p> <p>收快递：刚好碰上双十一，某公司的员工们在某宝/某东上购买了大量的商品，过了两天准备收快递了。这时候问题来了，怎么收快递呢？这时候就会有两种方案，1.全部的员工们都在公司门口等快递，2.让前台的小姐姐代签收。现实中我们都会采用委托的方法，也就是让前台的小姐姐代签（因为公司不会容忍那么多员工站在门口等快递）。</p> <p>而js中的事件委托则是使用事件冒泡来实现的。</p> <p>现在我们要实现一个点击li标签，弹出123的功能</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>ul1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>111<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>222<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>333<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>444<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>普通写法，遍历</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">var</span> oUl <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">&quot;ul1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> aLi <span class="token operator">=</span> oUl<span class="token punctuation">.</span><span class="token function">getElementsByTagName</span><span class="token punctuation">(</span><span class="token string">'li'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>aLi<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        aLi<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token function">alert</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>事件委托写法</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>window<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">var</span> oUl <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">&quot;ul1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   	oUl<span class="token punctuation">.</span><span class="token function-variable function">onclick</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">alert</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h2 id="_10-语义化"><a href="#_10-语义化" class="header-anchor">#</a> 10.语义化</h2> <h3 id="_10-1-语义化的特点"><a href="#_10-1-语义化的特点" class="header-anchor">#</a> 10.1 语义化的特点</h3> <ul><li>易修改、易维护。</li> <li>无障碍阅读支持。</li> <li>搜索引擎友好，利于 SEO。</li> <li>面向未来的 HTML，浏览器在未来可能提供更丰富的支持。</li></ul> <h3 id="_10-2-结构语义化"><a href="#_10-2-结构语义化" class="header-anchor">#</a> 10.2 结构语义化</h3> <p>语义元素均有一个共同特点——他们均不做任何事情。换句话说，语义元素仅仅是页面结构的规范化，并不会对内容有本质的影响。</p> <p>下图展示了一个典型的页面结构。</p> <p><img src="/assets/img/image-20200819095052828.40f33a04.png" alt="image-20200819095052828"></p> <h4 id="_10-2-1-头部"><a href="#_10-2-1-头部" class="header-anchor">#</a> 10.2.1 头部</h4> <p><code>元素有两种用法，第一是标注内容的标题，第二是标注网页的页眉，如上图你看到的那样。除非必要（内容标题附带其它信息的情况下：发布时间、作者等），一般不在内容中使用</code>。因而，网页中可以包含多个<code>元素。按照 HTML5 的规定，</code>都应包含某个级别的标题，所以应隐式或显式地包含标题，通常将不希望显示的标题设置为<code>display: none;</code>，一方面遵守规范，另一方面则提供了无障碍阅读而不至于影响到页面设计。</p> <h4 id="_10-2-2-导航栏"><a href="#_10-2-2-导航栏" class="header-anchor">#</a> 10.2.2 导航栏</h4> <p>导航栏使用<code>&lt;nav&gt;</code>看起来是理所当然的，进一步，它也用于一组文章的链接。一个页面可以包含多个<code>&lt;nav&gt;</code>元素，但通常仅仅在页面的主要导航部分使用它。
《HTML5：The Missing Manual》中指出了在侧栏使用<code>&lt;nav&gt;</code>标签的两个案例：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;!-- 案例一 --&gt;
&lt;nav&gt;
  &lt;!-- 此处是链接 --&gt;
  &lt;aside&gt;&lt;/aside&gt;
  &lt;aside&gt;&lt;/aside&gt;
&lt;/nav&gt;

&lt;!-- 案例二 --&gt;
&lt;aside&gt;
  &lt;nav&gt;
    &lt;!-- 此处是链接 --&gt;
  &lt;/nav&gt;
  &lt;section&gt;&lt;/section&gt;
  &lt;div&gt;&lt;/div&gt;
&lt;/aside&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>如果侧栏中包含其它不同于链接的其它区块，那么，使用第二种方案显然更为合适。</p> <p>导航通常包含一组链接，普遍认为，链接使用列表来组织。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;nav&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot; title=&quot;链接&quot;&gt;链接&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot; title=&quot;链接&quot;&gt;链接&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot; title=&quot;链接&quot;&gt;链接&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/nav&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h4 id="_10-2-3-附注"><a href="#_10-2-3-附注" class="header-anchor">#</a> 10.2.3 附注</h4> <p><code>&lt;aside&gt;</code>元素并不仅仅是侧栏，它表示与它周围文本没有密切关系的内容。文章中同样可以使用<code>&lt;aside&gt;</code>元素，来说明文章的附加内容、解释说明某个观点、相关内容链接等等。</p> <p>当<code>&lt;aside&gt;</code>用于侧栏时，其表示整个网页的附加内容。通常的广告区域、搜索、分享链接则位于侧栏。侧栏中的<code>&lt;section&gt;</code>元素规定了一个区域，通常是带有标题的内容。</p> <p><code>&lt;section&gt;</code>标签适合标记的内容区块：</p> <ul><li>与页面主体并列显示的小内容块。</li> <li>独立性内容，清单、表单等。</li> <li>分组内容，如 CMS 系统中的文章分类区块。</li> <li>比较长文档的一部分，可能仅仅是为了正确规定页面大纲。</li></ul> <div>标签依然是可用的，当你觉得使用其它标签都不太合适的时候。新的语义元素出现之前，我们总是这么干的！
<h4 id="_10-2-4-页脚"><a href="#_10-2-4-页脚" class="header-anchor">#</a> 10.2.4 页脚</h4> <p>同可“包罗万象”的<code>&lt;header&gt;</code>元素不同，标准规定<code>&lt;footer&gt;</code>标签仅仅可以包含版权、来源信息、法律限制等等之类的文本或链接信息。如果想要在页脚中包含其它内容，可以使用熟悉的<code>&lt;div&gt;</code>来帮忙。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;div&gt;
  &lt;aside&gt;
  &lt;!-- 其它内容 --&gt;
  &lt;/aside&gt;
  
  &lt;footer&gt;
    &lt;!-- 法律、版权、来源、联系信息等 --&gt;
  &lt;/footer&gt;
&lt;/div&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h4 id="_10-2-5-主要内容"><a href="#_10-2-5-主要内容" class="header-anchor">#</a> 10.2.5 主要内容</h4> <p>在早先的 HTML5 版本中并没有规定页面主体的标签，相关的书中经常会说：除去头部、尾部、侧栏等其它部分，剩下的自然是主体部分。</p> <p>然而，HTML5.1 中规定了一个<code>&lt;main&gt;</code>标签来标识主体内容。<code>&lt;main&gt;</code>标签不能包含在页面其它区块元素中，通常是<code>&lt;body&gt;</code>的子标签，或者是全局<code>&lt;div&gt;</code>的子标签。<code>&lt;main&gt;</code>标签可以帮助屏幕阅读工具识别页面的主体部分，从而让访问者迅速得到有用的信息。</p> <h4 id="_10-2-6-文章"><a href="#_10-2-6-文章" class="header-anchor">#</a> 10.2.6 文章</h4> <p><code>&lt;article&gt;</code>表示一个完整的、自成一体的内容块。如文章或新闻报道。<code>&lt;article&gt;</code>应包含完整的标题、文章署名、发布时间、正文。当语义与表现发生冲突，例如有时需要将文章分多个页面显示，那么需要把每个页面的文章区域都用<code>&lt;article&gt;</code>标记。</p> <p>文章中包含插图时，使用新的语义元素<code>&lt;figure&gt;</code>标签。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;article&gt;
  &lt;h1&gt;标题&lt;/h1&gt;
  &lt;p&gt;
    &lt;!-- 内容 --&gt;
  &lt;/p&gt;
  &lt;figure&gt;
    &lt;img src=&quot;#&quot; alt=&quot;插图&quot;&gt;
    &lt;figcaption&gt;这是一个插图&lt;/figcaption&gt;
  &lt;/figure&gt;
&lt;/article&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>上述情况下，<code>&lt;figcaption&gt;</code>包含了关于插图的详细解释，则<code>&lt;img&gt;</code>的<code>alt</code>属性可以略去。</p> <h3 id="_10-3-ife任务"><a href="#_10-3-ife任务" class="header-anchor">#</a> 10.3 IFE任务</h3> <p>IFE任务既是完成一个HTML页面代码编写（不写CSS，不需要关注样式，只关注文档结构）。</p> <h2 id="_11-bfc"><a href="#_11-bfc" class="header-anchor">#</a> 11.BFC</h2> <h3 id="_11-1-什么是bfc？"><a href="#_11-1-什么是bfc？" class="header-anchor">#</a> 11.1 什么是BFC？</h3> <blockquote><p>W3C对BFC的定义如下： 浮动元素和绝对定位元素，非块级盒子的块级容器（例如 inline-blocks, table-cells, 和  table-captions），以及overflow值不为&quot;visiable&quot;的块级盒子，都会为他们的内容创建新的BFC（Block  Fromatting Context， 即块级格式上下文）。</p></blockquote> <h3 id="_11-2-触发条件"><a href="#_11-2-触发条件" class="header-anchor">#</a> 11.2 触发条件</h3> <p>一个HTML元素要创建BFC，则满足下列的任意一个或多个条件即可： 下列方式会创建块格式化上下文：</p> <ul><li>根元素()</li> <li>浮动元素（元素的 float 不是 none）</li> <li>绝对定位元素（元素的 position 为 absolute 或 fixed）</li> <li>行内块元素（元素的 display 为 inline-block）</li> <li>表格单元格（元素的 display为 table-cell，HTML表格单元格默认为该值）</li> <li>表格标题（元素的 display 为 table-caption，HTML表格标题默认为该值）</li> <li>匿名表格单元格元素（元素的 display为 table、table-row、  table-row-group、table-header-group、table-footer-group（分别是HTML  table、row、tbody、thead、tfoot的默认属性）或 inline-table）</li> <li>overflow 值不为 visible 的块元素 -弹性元素（display为 flex 或 inline-flex元素的直接子元素）</li> <li>网格元素（display为 grid 或 inline-grid 元素的直接子元素） 等等。</li></ul> <h2 id="_12-跨域"><a href="#_12-跨域" class="header-anchor">#</a> 12.跨域</h2> <blockquote><p>Q：为什么会出现跨域问题？
A：出于浏览器的同源策略限制，浏览器会拒绝跨域请求。
*注：严格的说，浏览器并不是拒绝所有的跨域请求，实际上拒绝的是跨域的读操作。浏览器的同源限制策略是这样执行的：</p> <ul><li>通常浏览器允许进行跨域写操作（Cross-origin writes），如链接，重定向；</li> <li>通常浏览器允许跨域资源嵌入（Cross-origin embedding），如 img、script 标签；</li> <li>通常浏览器不允许跨域读操作（Cross-origin reads）。*</li></ul> <p>Q：什么情况才算作跨域？
A：非同源请求，均为跨域。名词解释：<em>同源 —— 如果两个页面拥有相同的协议（protocol），端口（port）和主机（host），那么这两个页面就属于同一个源（origin）。</em></p> <p><img src="/assets/img/image-20200819105130279.c0c78aff.png" alt="image-20200819105130279"></p> <p>Q：为什么有跨域需求?
A：场景 —— 工程服务化后，不同职责的服务分散在不同的工程中，往往这些工程的域名是不同的，但一个需求可能需要对应到多个服务，这时便需要调用不同服务的接口，因此会出现跨域。</p></blockquote> <p>那我们应该如何解决跨域问题呢？目前的常用的方法有JSONP，CORS，nginx方向代理，node中间件代理，websocket等</p> <h3 id="_12-1-jsonp"><a href="#_12-1-jsonp" class="header-anchor">#</a> 12.1 JSONP</h3> <p><strong>单纯地为了实现跨域请求而创造的一个 trick。</strong>
【实现原理】
虽然因为同源策略的影响，不能通过XMLHttpRequest请求不同域上的数据（Cross-origin reads）。但是，在页面上引入不同域上的js脚本文件却是可以的（Cross-origin embedding）。因此在js文件载入完毕之后，触发回调，可以将需要的data作为参数传入。
【实现方式（需前后端配合）】</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;script type=&quot;text/javascript&quot;&gt;
    function dosomething(data){
        //处理获得的数据
    }
&lt;/script&gt;
&lt;script src=&quot;http://example.com/data.php?callback=dosomething&quot;&gt;&lt;/script&gt;复制代码
&lt;?php
$callback = $_GET['callback'];//得到回调函数名
$data = array('a','b','c');//要返回的数据
echo $callback.'('.json_encode($data).')';//输出
?&gt;复制代码
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>【JSONP的优缺点】
优点：兼容性好（兼容低版本IE）
缺点：1.JSONP只支持GET请求； 2.XMLHttpRequest相对于JSONP有着更好的错误处理机制</p> <h3 id="_12-2-cors"><a href="#_12-2-cors" class="header-anchor">#</a> 12.2 CORS</h3> <p>CORS 是W3C 推荐的一种新的官方方案，能使服务器支持 XMLHttpRequest 的跨域请求。CORS 实现起来非常方便，只需要增加一些 HTTP 头，让服务器能声明允许的访问来源。</p> <div class="language-JavaScript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> express <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'express'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
<span class="token keyword">var</span> router <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
  
<span class="token comment">/* GET home page. */</span>  
router<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>  
  res<span class="token punctuation">.</span><span class="token function">render</span><span class="token punctuation">(</span><span class="token string">'index'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token string">'Express 路由1'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
  
<span class="token comment">/* GET home page. */</span>  
router<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'/cors'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>  
  res<span class="token punctuation">.</span><span class="token function">render</span><span class="token punctuation">(</span><span class="token string">'test/index'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token string">'Express 路由1'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
  
<span class="token comment">/* GET home page. */</span>  
router<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'/getData'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>  
  <span class="token comment">//设置允许跨域请求  </span>
  <span class="token keyword">var</span> reqOrigin <span class="token operator">=</span> req<span class="token punctuation">.</span><span class="token function">header</span><span class="token punctuation">(</span><span class="token string">&quot;origin&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
  
  <span class="token keyword">if</span><span class="token punctuation">(</span>reqOrigin <span class="token operator">!=</span><span class="token keyword">undefined</span> <span class="token operator">&amp;&amp;</span> reqOrigin<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">&quot;http://localhost:3000&quot;</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span>  
  <span class="token comment">//设置允许 http://localhost:3000 这个域响应  </span>
    res<span class="token punctuation">.</span><span class="token function">header</span><span class="token punctuation">(</span><span class="token string">&quot;Access-Control-Allow-Origin&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;http://localhost:3000&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
    res<span class="token punctuation">.</span><span class="token function">header</span><span class="token punctuation">(</span><span class="token string">&quot;Access-Control-Allow-Methods&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;PUT,POST,GET,DELETE,OPTIONS&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
    res<span class="token punctuation">.</span><span class="token function">header</span><span class="token punctuation">(</span><span class="token string">&quot;Access-Control-Allow-Headers&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Content-Type,Content-Length, Authorization, Accept,X-Requested-With&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
  <span class="token punctuation">}</span>  
  res<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>name<span class="token operator">:</span><span class="token string">&quot;张三1&quot;</span><span class="token punctuation">,</span>age<span class="token operator">:</span><span class="token number">40</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
  
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
  
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> router<span class="token punctuation">;</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><h3 id="_12-3-nginx反向代理"><a href="#_12-3-nginx反向代理" class="header-anchor">#</a> 12.3 nginx反向代理</h3> <p>使用nginx反向代理实现跨域，是最简单的跨域方式。只需要修改nginx的配置即可解决跨域问题，支持所有浏览器，支持session，不需要修改任何代码，并且不会影响服务器性能。</p> <p><strong>思路</strong>：通过nginx配置一个代理服务器（域名与domain1相同，端口不同）做跳板机，反向代理访问domain2接口，并且可以顺便修改cookie中domain信息，方便当前域cookie写入，实现跨域登录。</p> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">location</span> <span class="token operator">/</span>Api <span class="token punctuation">{</span>
    <span class="token keyword">proxy_redirect</span> off<span class="token punctuation">;</span>
    <span class="token keyword">proxy_set_header</span> Host <span class="token variable">$host</span><span class="token punctuation">;</span>
    <span class="token keyword">proxy_set_header</span> X<span class="token operator">-</span>Real<span class="token operator">-</span>IP <span class="token variable">$remote_addr</span><span class="token punctuation">;</span>
    <span class="token keyword">proxy_set_header</span> X<span class="token operator">-</span>Forwarded<span class="token operator">-</span>For <span class="token variable">$proxy_add_x_forwarded_for</span><span class="token punctuation">;</span>
    <span class="token keyword">proxy_pass</span>  <span class="token keyword">http</span><span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>test<span class="token punctuation">.</span>com<span class="token punctuation">:</span><span class="token number">8001</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_12-4-node中间件代理"><a href="#_12-4-node中间件代理" class="header-anchor">#</a> 12.4 node中间件代理</h3> <p>与nginx代理的原理如出一辙，只不过是使用了node中间件作为了代理。如vuecli中的代理就是使用了node中间件进行了代理。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">//vue.config.js</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    proxy<span class="token operator">:</span><span class="token punctuation">{</span>
    	<span class="token string">'/api'</span><span class="token operator">:</span> <span class="token punctuation">{</span>
          target<span class="token operator">:</span> <span class="token string">'http://127.0.0.1:8001'</span><span class="token punctuation">,</span>
          ws<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
          changeOrigin<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
          pathRewrite<span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token string">'^/api'</span><span class="token operator">:</span> <span class="token string">''</span>
          <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="_12-5-websocket"><a href="#_12-5-websocket" class="header-anchor">#</a> 12.5 websocket</h3> <p>WebSocket是一种通信协议，使用<code>ws://</code>（非加密）和<code>wss://</code>（加密）作为协议前缀。该协议不实行同源政策，只要服务器支持，就可以通过它进行跨源通信。</p> <h3 id="_13-es6之set和map"><a href="#_13-es6之set和map" class="header-anchor">#</a> 13.ES6之set和map</h3> <h4 id="_13-1-set-集合-类数组"><a href="#_13-1-set-集合-类数组" class="header-anchor">#</a> 13.1 Set (集合)[类数组]</h4> <ul><li><p>集合是由一组无序且唯一(即不能重复)的项组成的，可以想象成集合是一个既没有重复元素，也没有顺序概念的数组</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string">&quot;1&quot;</span><span class="token operator">:</span> <span class="token string">&quot;2&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">&quot;3&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;4&quot;</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>ES6提供了新的数据结构Set。它类似于数组，但是成员的值都是唯一的，没有重复的值</p></li> <li><p>Set 本身是一个构造函数，用来生成 Set 数据结构</p> <p>这里说的Set其实就是我们所要讲到的集合，先来看下基础用法</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> s <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=&gt;</span> s<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token keyword">of</span> s<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 2 3 5 4</span>
<span class="token punctuation">}</span>

<span class="token comment">// 去除数组的重复成员</span>
<span class="token keyword">let</span> array <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span><span class="token operator">...</span><span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span>array<span class="token punctuation">)</span><span class="token punctuation">]</span>     <span class="token comment">// [1, 2, 4, 5, 3]</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div></li></ul> <h5 id="方法和属性总结"><a href="#方法和属性总结" class="header-anchor">#</a> 方法和属性总结</h5> <ul><li><p>属性</p> <p>size：返回字典所包含的元素个数</p></li> <li><p>操作方法</p> <ol><li><p>add(value)：添加某个值，返回 Set 结构本身。</p></li> <li><p>delete(value)：删除某个值，返回一个布尔值，表示删除是否成功。</p></li> <li><p>has(value)：返回一个布尔值，表示该值是否为 Set 的成员。</p></li> <li><p>clear()：清除所有成员，无返回值。</p></li> <li><p>size: 返回set数据结构的数据长度</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> <span class="token keyword">set</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">set</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Set [ 1, 2 ]</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">set</span><span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">set</span><span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">set</span><span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// undefined</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">set</span><span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">set</span><span class="token punctuation">.</span>size<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// 0</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li></ol></li> <li><p>遍历方法</p> <ol><li><p>keys()：返回键名的遍历器</p></li> <li><p>values()：返回键值的遍历器</p></li> <li><p>entries()：返回键值对的遍历器</p></li> <li><p>forEach()：使用回调函数遍历每个成员，无返回值</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> <span class="token keyword">set</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">set</span><span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// SetIterator {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">...</span><span class="token keyword">set</span><span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> <span class="token keyword">set</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">set</span><span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// SetIterator {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">...</span><span class="token keyword">set</span><span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> <span class="token keyword">set</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">set</span><span class="token punctuation">.</span><span class="token function">entries</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// SetIterator {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">...</span><span class="token keyword">set</span><span class="token punctuation">.</span><span class="token function">entries</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [[&quot;a&quot;, &quot;a&quot;], [&quot;b&quot;, &quot;b&quot;], [&quot;c&quot;, &quot;c&quot;]]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> <span class="token keyword">set</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">set</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">,</span> key</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>key <span class="token operator">+</span> <span class="token string">': '</span> <span class="token operator">+</span> value<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 1: 1</span>
<span class="token comment">// 2: 2</span>
<span class="token comment">// 3: 3</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li></ol></li></ul> <h3 id="_13-2-map-字典-类对象"><a href="#_13-2-map-字典-类对象" class="header-anchor">#</a> 13.2 Map (字典)[类对象]</h3> <p>它类似于对象，也是键值对的集合，但是“键”的范围不限于字符串，各种类型的值（包括对象）都可以当作键，是一种更完善的 Hash 结构实现。如果你需要“键值对”的数据结构，Map 比 Object 更合适；</p> <blockquote><p>那么集合又和字典有什么区别呢：</p></blockquote> <ul><li><p>共同点：集合、字典可以存储不重复的值</p></li> <li><p>不同点：集合是以[值，值]的形式存储元素，字典是以[键，值]的形式存储</p> <p>这里说的Set其实就是我们所要讲到的集合，先来看下基础用法</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> m <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Map</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> o <span class="token operator">=</span> <span class="token punctuation">{</span>p<span class="token operator">:</span> <span class="token string">'Hello World'</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
m<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>o<span class="token punctuation">,</span> <span class="token string">'content'</span><span class="token punctuation">)</span>
m<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token comment">// &quot;content&quot;</span>

m<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token comment">// true</span>
m<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token comment">// true</span>
m<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span>o<span class="token punctuation">)</span> <span class="token comment">// false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div></li></ul> <h5 id="方法和属性总结-2"><a href="#方法和属性总结-2" class="header-anchor">#</a> 方法和属性总结</h5> <ul><li><p>属性</p> <p>size：返回字典所包含的元素个数</p></li> <li><p>操作方法</p> <ol><li><p>set(key, val): 向字典中添加新元素</p></li> <li><p>get(key):通过键值查找特定的数值并返回</p></li> <li><p>has(key):如果键存在字典中返回true,否则false</p></li> <li><p>delete(key): 通过键值从字典中移除对应的数据</p></li> <li><p>clear():将这个字典中的所有元素删除</p></li> <li><div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">let</span> m <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Map</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
m<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'Jay'</span><span class="token punctuation">,</span> <span class="token string">'Jay的Chou'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
m<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token string">'真的'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token string">'Chou'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span>size<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 2</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [ 'Jay', 'true' ]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [ 'Jay的Chou', '真的' ]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'jay'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// undefined</span>

m<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [ 'Jay' ]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>m<span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [ 'Jay的Chou' ]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><blockquote><p>由于一个key只能对应一个value，所以，多次对一个key放入value，后面的值会把前面的值冲掉：</p></blockquote> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> m <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Map</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
m<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'Adam'</span><span class="token punctuation">,</span> <span class="token number">67</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
m<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">'Adam'</span><span class="token punctuation">,</span> <span class="token number">88</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
m<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">'Adam'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 88</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><blockquote><p>Map方法接受一个二维数组当做参数</p></blockquote> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> m <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Map</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'zhangsan'</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'sex'</span><span class="token punctuation">,</span> <span class="token string">'male'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//Map {&quot;name&quot; =&gt; &quot;zhangsan&quot;, &quot;sex&quot; =&gt; &quot;male&quot;}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ol></li> <li><p>遍历方法</p> <ol><li><p>keys():将字典中包含的所有键名以数组形式返回</p></li> <li><p>values():将字典中包含的所有数值以数组形式返回</p></li> <li><p>entries()：返回键值对的遍历器</p></li> <li><p>forEach()：遍历字典的所有成员</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Map</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string">'age'</span><span class="token punctuation">,</span> <span class="token number">19</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'height'</span><span class="token punctuation">,</span> <span class="token number">180</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> key <span class="token keyword">of</span> map<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// age height</span>
<span class="token punctuation">}</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> value <span class="token keyword">of</span> map<span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//19 180</span>
<span class="token punctuation">}</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> item <span class="token keyword">of</span> map<span class="token punctuation">.</span><span class="token function">entries</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> item<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> item<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//age: 19  height: 180</span>
<span class="token punctuation">}</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> <span class="token punctuation">[</span>key<span class="token punctuation">,</span> value<span class="token punctuation">]</span> <span class="token keyword">of</span> map<span class="token punctuation">.</span><span class="token function">entries</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> key <span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> value <span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//age: 19  height: 180</span>
<span class="token punctuation">}</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> <span class="token punctuation">[</span>key<span class="token punctuation">,</span> value<span class="token punctuation">]</span> <span class="token keyword">of</span> map<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> key <span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> value <span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//age: 19  height: 180</span>
<span class="token punctuation">}</span>

map<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">value<span class="token punctuation">,</span> key<span class="token punctuation">,</span> map</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> key <span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> value <span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//age: 19  height: 180</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div></li></ol></li></ul> <h2 id="_14-new操作符"><a href="#_14-new操作符" class="header-anchor">#</a> 14.new操作符</h2> <p>new操作符具体干了什么？</p> <ol><li>创建一个新对象。</li> <li>这个新对象的<code>__proto__</code>属性指向原函数的<code>prototype</code>属性。(即继承原函数的原型)。</li> <li>将构造函数的<code>this</code>绑定到新创建的对象上去，并使用<code>apply</code>执行构造器函数，这样构造函数的属性和方法就被添加到新创建的对象中去了。</li> <li>返回新对象，如果构造函数有返回对象就将此对象返回，否则，直接返回第一步创建的对象。</li></ol> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 1、Con: 接收一个构造函数</span>
<span class="token comment">// 2、args：传入构造函数的参数</span>
<span class="token keyword">function</span> <span class="token function">create</span><span class="token punctuation">(</span><span class="token parameter">Con<span class="token punctuation">,</span> <span class="token operator">...</span>args</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">let</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    obj<span class="token punctuation">.</span>_proto_ <span class="token operator">=</span> <span class="token class-name">Con</span><span class="token punctuation">.</span>prototype<span class="token punctuation">;</span>
    <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token function">Con</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span>args<span class="token punctuation">)</span>
    <span class="token keyword">return</span> result <span class="token keyword">instanceof</span> <span class="token class-name">Object</span> <span class="token operator">?</span>  result <span class="token operator">:</span> obj<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// 构造函数</span>
<span class="token keyword">function</span> <span class="token function">Test</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name
    <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age
<span class="token punctuation">}</span>
<span class="token class-name">Test</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">sayName</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment">// 实现一个 new 操作符</span>
<span class="token keyword">const</span> a <span class="token operator">=</span> <span class="token function">create</span><span class="token punctuation">(</span>Test<span class="token punctuation">,</span><span class="token string">'小鹿'</span><span class="token punctuation">,</span><span class="token string">'23'</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>age<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h2 id="_15-js创造对象"><a href="#_15-js创造对象" class="header-anchor">#</a> 15.js创造对象</h2> <h4 id="_15-1-object构造函数或对象字面量"><a href="#_15-1-object构造函数或对象字面量" class="header-anchor">#</a> 15.1 Object构造函数或对象字面量</h4> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// object构造函数</span>
<span class="token keyword">var</span> person <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
person<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">&quot;Nicholas&quot;</span><span class="token punctuation">;</span> 
person<span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token number">29</span><span class="token punctuation">;</span> 
person<span class="token punctuation">.</span>job <span class="token operator">=</span> <span class="token string">&quot;Software Engineer&quot;</span><span class="token punctuation">;</span> 
person<span class="token punctuation">.</span><span class="token function-variable function">sayName</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
 <span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">// 对象字面量</span>
<span class="token keyword">var</span> person <span class="token operator">=</span> <span class="token punctuation">{</span> 
 name<span class="token operator">:</span> <span class="token string">&quot;Nicholas&quot;</span><span class="token punctuation">,</span> 
 age<span class="token operator">:</span> <span class="token number">29</span><span class="token punctuation">,</span> 
 job<span class="token operator">:</span> <span class="token string">&quot;Software Engineer&quot;</span><span class="token punctuation">,</span> 
 <span class="token function-variable function">sayName</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
   <span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
 <span class="token punctuation">}</span> 
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><blockquote><p>创建单个对象非常简洁，这两种对象创建方式特别是对象字面量的方式是平时写业务代码中最常见的对象创建方式，不过如果要创建多个对象会产生大量重复代码。</p></blockquote> <h4 id="_15-2-工厂模式"><a href="#_15-2-工厂模式" class="header-anchor">#</a> 15.2 工厂模式</h4> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">createPerson</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age<span class="token punctuation">,</span> job</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
 <span class="token keyword">var</span> o <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
 o<span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> 
 o<span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> 
 o<span class="token punctuation">.</span>job <span class="token operator">=</span> job<span class="token punctuation">;</span> 
 o<span class="token punctuation">.</span><span class="token function-variable function">sayName</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
   <span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
 <span class="token punctuation">}</span><span class="token punctuation">;</span> 
 <span class="token keyword">return</span> o<span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
<span class="token keyword">var</span> person1 <span class="token operator">=</span> <span class="token function">createPerson</span><span class="token punctuation">(</span><span class="token string">&quot;Nicholas&quot;</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">,</span> <span class="token string">&quot;Software Engineer&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token keyword">var</span> person2 <span class="token operator">=</span> <span class="token function">createPerson</span><span class="token punctuation">(</span><span class="token string">&quot;Greg&quot;</span><span class="token punctuation">,</span> <span class="token number">27</span><span class="token punctuation">,</span> <span class="token string">&quot;Doctor&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><blockquote><p>工厂模式解决了创建多个对象产生重复代码的问题，但由于所有的实例都是有<code>object</code>实例化而来，没有解决对象识别的问题，所有实例都是<code>object</code>类型。</p></blockquote> <h4 id="_15-3-构造函数模式"><a href="#_15-3-构造函数模式" class="header-anchor">#</a> 15.3 构造函数模式</h4> <p>为了解决类型识别问题，我们引入了构造函数模式，即先定义好一个构造函数，在函数里定义好属性与方法，然后通过<code>new</code>关键字用该构造函数实例化一个对象。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age<span class="token punctuation">,</span> job</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span>job <span class="token operator">=</span> job<span class="token punctuation">;</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function-variable function">sayName</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
   <span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
 <span class="token punctuation">}</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
<span class="token keyword">var</span> person1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">&quot;Nicholas&quot;</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">,</span> <span class="token string">&quot;Software Engineer&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token keyword">var</span> person2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">&quot;Greg&quot;</span><span class="token punctuation">,</span> <span class="token number">27</span><span class="token punctuation">,</span> <span class="token string">&quot;Doctor&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><blockquote><p>由于不同类型的对象可以定义不同的构造函数，所以这种模式解决了对象类型识别的问题，但这种模式还是存在一些问题，即一个构造函数多次实例化的时候，它定义的方法是不能复用的，还是存在重复代码的问题。</p></blockquote> <h4 id="_15-4-原型模式"><a href="#_15-4-原型模式" class="header-anchor">#</a> 15.4 原型模式</h4> <p>为了解决构造函数模式中方法重复定义的问题，我们引入了原型模式，与构造函数模式相比，原型模式将构造函数的属性和方法全部定义到到构造函数的原型对象上去。这样通过构造函数实例化的所有实例，共用一套构造函数原型对象上的属性与方法。通过某个实例对构造函数原型对象上任何属性的修改，都会影响到其他的实例对象。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
<span class="token punctuation">}</span> 
<span class="token class-name">Person</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">&quot;Nicholas&quot;</span><span class="token punctuation">;</span> 
<span class="token class-name">Person</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token number">29</span><span class="token punctuation">;</span> 
<span class="token class-name">Person</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>job <span class="token operator">=</span> <span class="token string">&quot;Software Engineer&quot;</span><span class="token punctuation">;</span> 
<span class="token class-name">Person</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">sayName</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
 <span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span><span class="token punctuation">;</span> 
<span class="token keyword">var</span> person1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
person1<span class="token punctuation">.</span><span class="token function">sayName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//&quot;Nicholas&quot; </span>
<span class="token keyword">var</span> person2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
person2<span class="token punctuation">.</span><span class="token function">sayName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//&quot;Nicholas&quot; </span>
<span class="token function">alert</span><span class="token punctuation">(</span>person1<span class="token punctuation">.</span>sayName <span class="token operator">==</span> person2<span class="token punctuation">.</span>sayName<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><blockquote><p>原型模式实现了属性与方法的公用，但我们其实想要的只是方法的共用，对象属性我们希望也应该是各个实例对象私有的，在一个实例对象上对其属性的任何修改，不应该对其他实例对象有影响。这是原型模式的一个缺点。</p></blockquote> <h4 id="_15-5-组合使用构造函数模式和原型模式"><a href="#_15-5-组合使用构造函数模式和原型模式" class="header-anchor">#</a> 15.5 组合使用构造函数模式和原型模式</h4> <p>为了解决原型模式中属性共用的问题，同时保持原型模式的其他优点。我们试着将构造函数模式和原型模式组合起来，具体方式为以构造函数为基础，对象属性还是在构造函数中定义，但是将方法移到构造函数外部，并将其定义到构造函数的原型对象上去。这样我们每次在通过<code>new</code>关键字实例化一个对象的时候，都会重新创建一份实例的属性，不同的实例拥有各自的属性副本，彼此无任何关联。同时，由于在构造函数外部将对象方法定义到了构造函数的实例对象上去，所以这些方法只在函数的原型对象上定义一次，就可以让由这个构造函数实例化的所有实例对象共用。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age<span class="token punctuation">,</span> job</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span>job <span class="token operator">=</span> job<span class="token punctuation">;</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span>friends <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;Shelby&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Court&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
<span class="token class-name">Person</span><span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token punctuation">{</span> 
 constructor <span class="token operator">:</span> Person<span class="token punctuation">,</span> 
 <span class="token function-variable function">sayName</span> <span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
 <span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
 <span class="token punctuation">}</span> 
<span class="token punctuation">}</span> 
<span class="token keyword">var</span> person1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">&quot;Nicholas&quot;</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">,</span> <span class="token string">&quot;Software Engineer&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token keyword">var</span> person2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">&quot;Greg&quot;</span><span class="token punctuation">,</span> <span class="token number">27</span><span class="token punctuation">,</span> <span class="token string">&quot;Doctor&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><blockquote><p>组合使用构造函数与原型模式，既解决了原型模式中方法公用问题又让每个实例对象拥有自己独立的属性，是现在对象创建最常见的方式。</p></blockquote> <h4 id="_15-6-动态原型模式"><a href="#_15-6-动态原型模式" class="header-anchor">#</a> 15.6 动态原型模式</h4> <p>另外还有一种动态原型模式，它是在组合使用构造函数与原型模式的基础上，再次做了一点改进，即将构造函数方法的定义移到函数里面去，还是在够到函数的原型对象上定义方法，同时为了防止每次调用构造函数都执行重新定义方法的问题，会在定义方法前先判断下构造函数的原型对象上是否已经存在该方法，如果存在就不会重复定义。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age<span class="token punctuation">,</span> job</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
     <span class="token comment">//属性</span>
     <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> 
     <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> 
     <span class="token keyword">this</span><span class="token punctuation">.</span>job <span class="token operator">=</span> job<span class="token punctuation">;</span>
     <span class="token comment">//方法</span>
     <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> <span class="token keyword">this</span><span class="token punctuation">.</span>sayName <span class="token operator">!=</span> <span class="token string">&quot;function&quot;</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
         <span class="token class-name">Person</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">sayName</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
         <span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
    <span class="token punctuation">}</span>
 <span class="token punctuation">}</span> 
<span class="token punctuation">}</span> 
<span class="token keyword">var</span> friend <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token string">&quot;Nicholas&quot;</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">,</span> <span class="token string">&quot;Software Engineer&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
friend<span class="token punctuation">.</span><span class="token function">sayName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><blockquote><p>相较于组合模式，动态原型模式在更加简洁，所有代码全部封装在构造函数里，风格很优雅。</p></blockquote> <h2 id="_16-js继承"><a href="#_16-js继承" class="header-anchor">#</a> 16.js继承</h2> <h4 id="_16-1-原型链模式"><a href="#_16-1-原型链模式" class="header-anchor">#</a> 16.1 原型链模式</h4> <p>原型链是实现js继承的主要方法，它的基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法。实现的本质是重写原型对象，代之以一个新类型的实例。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">SuperType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
  <span class="token keyword">this</span><span class="token punctuation">.</span>property <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span>
<span class="token class-name">SuperType</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">getSuperValue</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
  <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>property<span class="token punctuation">;</span> 
<span class="token punctuation">}</span><span class="token punctuation">;</span> 
<span class="token keyword">function</span> <span class="token function">SubType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
  <span class="token keyword">this</span><span class="token punctuation">.</span>subproperty <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
<span class="token comment">// 继承了 SuperType </span>
<span class="token class-name">SubType</span><span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SuperType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token comment">// 下面方法必须放在SubType.prototype = new SuperType()之后，且不能用对象字面量形式新增方法，因为会切断原型链</span>
<span class="token class-name">SubType</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">getSubValue</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
 <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>subproperty<span class="token punctuation">;</span> 
<span class="token punctuation">}</span><span class="token punctuation">;</span> 
<span class="token keyword">var</span> instance <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SubType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token function">alert</span><span class="token punctuation">(</span>instance<span class="token punctuation">.</span><span class="token function">getSuperValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><blockquote><p>原型链的第一个问题是包含引用类型值的原型属性会被所有实例共享，这是不应该的；第二个问题是在创建子类型的实例时，没有办法在不影响所有对象实例的情况下，给超类型的构造函数传递参数。</p></blockquote> <h4 id="_16-2-借用构造函数"><a href="#_16-2-借用构造函数" class="header-anchor">#</a> 16.2 借用构造函数</h4> <p>为了解决原型链模式的原型中包含引用类型值所带来问题，我们引入另一种继承方式，借用构造函数。具体思路是在子类型构造函数的内部调用超类构造函数，具体方法是在子类构造函数中调对超类构造函数调用<code>apply()</code>或<code>call()</code>方法，并传入子类构造函数的上下文<code>this</code>，可实现在子类上下文环境下执行超类中定义的对象初始化代码。如此，可以在子类的每个实例中都具有一份超类的属性与方法副本。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">SuperType</span><span class="token punctuation">(</span><span class="token parameter">name</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function-variable function">sayName</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
   <span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
 <span class="token punctuation">}</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
<span class="token keyword">function</span> <span class="token function">SubType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
 <span class="token comment">//继承了 SuperType，同时还传递了参数</span>
 <span class="token function">SuperType</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> <span class="token string">&quot;Nicholas&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
 <span class="token comment">//实例属性</span>
 <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token number">29</span><span class="token punctuation">;</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function-variable function">sayAge</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
   <span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span> 
 <span class="token punctuation">}</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
<span class="token keyword">var</span> instance <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SubType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token function">alert</span><span class="token punctuation">(</span>instance<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//&quot;Nicholas&quot;; </span>
<span class="token function">alert</span><span class="token punctuation">(</span>instance<span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//29</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><blockquote><p>问题：优点是可以在子类型构造函数中向超类型构造函数传递参数。缺点是方法都在构造函数中定义，无法实现函数复用；在超类型的原型中定义的方法，对子类型而言也是不可见,相当于这种方式没有继承到父类的方法。</p></blockquote> <h4 id="_16-3-组合继承"><a href="#_16-3-组合继承" class="header-anchor">#</a> 16.3 组合继承</h4> <blockquote><p>为了克服以上借用构造函数方式的问题，我们引入了原型链与借用构造函数组合的继承方式，叫组合继承。具体思路是使用原型链实现对原型属性和方法的继承，而通过借用构造函数来实现对实例属性的继承。这样，既通过在原型上定义方法实现了函数复用，又能够保证每个实例都有它自己的属性。这样，既通过在原型上定义方法实现了函数复用，又能够保证每个实例都有它自己的属性。</p></blockquote> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">SuperType</span><span class="token punctuation">(</span><span class="token parameter">name</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span>colors <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;blue&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
<span class="token class-name">SuperType</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">sayName</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
 <span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span> 
<span class="token keyword">function</span> <span class="token function">SubType</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
 <span class="token comment">//继承属性</span>
 <span class="token function">SuperType</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
 <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
<span class="token comment">//继承方法</span>
<span class="token class-name">SubType</span><span class="token punctuation">.</span>prototype <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SuperType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token class-name">SubType</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>constructor <span class="token operator">=</span> SubType<span class="token punctuation">;</span> 
<span class="token class-name">SubType</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">sayAge</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
 <span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span><span class="token punctuation">;</span> 
<span class="token keyword">var</span> instance1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SubType</span><span class="token punctuation">(</span><span class="token string">&quot;Nicholas&quot;</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
instance1<span class="token punctuation">.</span>colors<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">&quot;black&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token function">alert</span><span class="token punctuation">(</span>instance1<span class="token punctuation">.</span>colors<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//&quot;red,blue,green,black&quot; </span>
instance1<span class="token punctuation">.</span><span class="token function">sayName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//&quot;Nicholas&quot;; </span>
instance1<span class="token punctuation">.</span><span class="token function">sayAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//29 </span>
<span class="token keyword">var</span> instance2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SubType</span><span class="token punctuation">(</span><span class="token string">&quot;Greg&quot;</span><span class="token punctuation">,</span> <span class="token number">27</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token function">alert</span><span class="token punctuation">(</span>instance2<span class="token punctuation">.</span>colors<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//&quot;red,blue,green&quot; </span>
instance2<span class="token punctuation">.</span><span class="token function">sayName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//&quot;Greg&quot;; </span>
instance2<span class="token punctuation">.</span><span class="token function">sayAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//27</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><blockquote><p>组合继承避免了原型链和借用构造函数的缺陷，融合了它们的优点，成为 JavaScript中最常用的继承模式。而且，instanceof 和 isPrototypeOf()也能够用于识别基于组合继承创建的对象。</p></blockquote> <blockquote><p>缺点：组合继承最大的 问题就是无论什么情况下，都会调用两次超类型构造函数：一次是在创建子类型原型的时候，另一次是 在子类型构造函数内部。没错，子类型最终会包含超类型对象的全部实例属性，但我们不得不在调用子 类型构造函数时重写这些属性。再</p></blockquote> <h4 id="_16-4-原型式继承"><a href="#_16-4-原型式继承" class="header-anchor">#</a> 16.4 原型式继承</h4> <p>在子类构造函数内部，先创建了一个临时性的构造函数，然后将传入的对象作为这个构造函数的原型，最后返回了这个临时类型的一个新实例。原型式继承无需调用new关键字，直接调用构造方法即可。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">object</span><span class="token punctuation">(</span><span class="token parameter">o</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
 <span class="token keyword">function</span> <span class="token constant">F</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> 
 <span class="token class-name">F</span><span class="token punctuation">.</span>prototype <span class="token operator">=</span> o<span class="token punctuation">;</span> 
 <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">F</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>ES5 通过新增 Object.create()方法规范化了原型式继承。这个方法接收两个参数：一 个用作新对象原型的对象和（可选的）一个为新对象定义额外属性的对象。在传入一个参数的情况下， Object.create()与 object()方法的行为相同</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> person <span class="token operator">=</span> <span class="token punctuation">{</span> 
 name<span class="token operator">:</span> <span class="token string">&quot;Nicholas&quot;</span><span class="token punctuation">,</span> 
 friends<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Shelby&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Court&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Van&quot;</span><span class="token punctuation">]</span> 
<span class="token punctuation">}</span><span class="token punctuation">;</span> 
<span class="token comment">// 第二个参数可选</span>
<span class="token keyword">var</span> anotherPerson <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span>person<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token comment">//var anotherPerson = Object.create(person, { </span>
<span class="token comment">// name: { </span>
<span class="token comment">// value: &quot;Greg&quot; </span>
<span class="token comment">// } </span>
<span class="token comment">// });</span>
anotherPerson<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">&quot;Greg&quot;</span><span class="token punctuation">;</span> 
anotherPerson<span class="token punctuation">.</span>friends<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">&quot;Rob&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><blockquote><p>如果只是简单得想让一个对象与另一个对象保持一致，可以用原型式继承，不过需特别注意，由于原型式继承本质上是将新对象的原型属性的指针指向被复制对象，所以即使在复制完后，任何对被复制对象的修改，都会反应到新对象上。</p></blockquote> <h4 id="_16-5-寄生式继承"><a href="#_16-5-寄生式继承" class="header-anchor">#</a> 16.5 寄生式继承</h4> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">createAnother</span><span class="token punctuation">(</span><span class="token parameter">original</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
     <span class="token keyword">var</span> clone <span class="token operator">=</span> <span class="token function">object</span><span class="token punctuation">(</span>original<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//通过调用函数创建一个新对象</span>
     clone<span class="token punctuation">.</span><span class="token function-variable function">sayHi</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//以某种方式来增强这个对象</span>
     <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">&quot;hi&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
 <span class="token punctuation">}</span><span class="token punctuation">;</span> 
 <span class="token keyword">return</span> clone<span class="token punctuation">;</span> <span class="token comment">//返回这个对象</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><blockquote><p>函数不能复用</p></blockquote> <h4 id="_16-6-寄生组合式继承"><a href="#_16-6-寄生组合式继承" class="header-anchor">#</a> 16.6 寄生组合式继承</h4> <p>通过借用构造函数来继承属性，通过原型链的混成形式来继承方法。其背后的基本思路是：不必为了指定子类型的原型而调用超类型的构造函数，我们所需要的无非就是超类型原型的一个副本而已。本质上，就是使用寄生式继承来继承超类型的原型，然后再将结果指定给子类型的原型。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">inheritPrototype</span><span class="token punctuation">(</span><span class="token parameter">subType<span class="token punctuation">,</span> superType</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
  <span class="token keyword">var</span> prototype <span class="token operator">=</span> <span class="token function">object</span><span class="token punctuation">(</span>superType<span class="token punctuation">.</span>prototype<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//创建对象</span>
  prototype<span class="token punctuation">.</span>constructor <span class="token operator">=</span> subType<span class="token punctuation">;</span> <span class="token comment">//增强对象</span>
  subType<span class="token punctuation">.</span>prototype <span class="token operator">=</span> prototype<span class="token punctuation">;</span> <span class="token comment">//指定对象</span>
<span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">SuperType</span><span class="token punctuation">(</span><span class="token parameter">name</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
  <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> 
  <span class="token keyword">this</span><span class="token punctuation">.</span>colors <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;blue&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
<span class="token class-name">SuperType</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">sayName</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
  <span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span><span class="token punctuation">;</span> 
<span class="token keyword">function</span> <span class="token function">SubType</span><span class="token punctuation">(</span><span class="token parameter">name<span class="token punctuation">,</span> age</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
  <span class="token function">SuperType</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
  <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
<span class="token function">inheritPrototype</span><span class="token punctuation">(</span>SubType<span class="token punctuation">,</span> SuperType<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token class-name">SubType</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">sayAge</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
  <span class="token function">alert</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><blockquote><p>这个例子的高效率体现在它只调用了一次<code>SuperType</code>构造函数，并且因此避免了在 <code>SubType.prototype</code>上面创建不必要的、多余的属性。与此同时，原型链还能保持不变；因此，还能够正常使用 <code>instanceof</code> 和 <code>isPrototypeOf()</code>。</p></blockquote></div></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2020-8-21 15:21:50</span></div></footer> <!----> <!----> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-40a3448a><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_1-数据类型" class="sidebar-link" data-v-40a3448a>1.数据类型</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_1-1-数据类型分类" class="sidebar-link" data-v-40a3448a>1.1 数据类型分类</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_1-2-类型判断" class="sidebar-link" data-v-40a3448a>1.2 类型判断</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_2-作用域" class="sidebar-link" data-v-40a3448a>2.作用域</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_2-1-执行上下文" class="sidebar-link" data-v-40a3448a>2.1 执行上下文</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_2-2-调用栈" class="sidebar-link" data-v-40a3448a>2.2 调用栈</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_2-3-变量提升" class="sidebar-link" data-v-40a3448a>2.3 变量提升</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_2-4-作用域" class="sidebar-link" data-v-40a3448a>2.4 作用域</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_2-5-作用域链" class="sidebar-link" data-v-40a3448a>2.5 作用域链</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_3-闭包" class="sidebar-link" data-v-40a3448a>3.闭包</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_3-1-概念" class="sidebar-link" data-v-40a3448a>3.1 概念</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_3-2-特点" class="sidebar-link" data-v-40a3448a>3.2 特点</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_3-3-闭包的创建" class="sidebar-link" data-v-40a3448a>3.3 闭包的创建</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_3-4-闭包的应用场景" class="sidebar-link" data-v-40a3448a>3.4 闭包的应用场景</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_4-this" class="sidebar-link" data-v-40a3448a>4.this</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_4-1-this的指向" class="sidebar-link" data-v-40a3448a>4.1 this的指向</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_4-2-改变this的指向" class="sidebar-link" data-v-40a3448a>4.2 改变this的指向</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_5-string-和-array-常用函数" class="sidebar-link" data-v-40a3448a>5.String 和 Array 常用函数</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_5-1-string" class="sidebar-link" data-v-40a3448a>5.1 String</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_5-2-array" class="sidebar-link" data-v-40a3448a>5.2 Array</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_6-堆内存和栈内存" class="sidebar-link" data-v-40a3448a>6. 堆内存和栈内存</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_7-浅拷贝和深拷贝" class="sidebar-link" data-v-40a3448a>7.浅拷贝和深拷贝</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_8-原型和原型链" class="sidebar-link" data-v-40a3448a>8.原型和原型链</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_8-1-prototype" class="sidebar-link" data-v-40a3448a>8.1 prototype</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_8-2-proto" class="sidebar-link" data-v-40a3448a>8.2 __proto__</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_8-3-constructor" class="sidebar-link" data-v-40a3448a>8.3 constructor</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_8-4-实例与原型" class="sidebar-link" data-v-40a3448a>8.4 实例与原型</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_8-5-原型的原型" class="sidebar-link" data-v-40a3448a>8.5 原型的原型</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_8-6-原型链" class="sidebar-link" data-v-40a3448a>8.6 原型链</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_9-冒泡和捕获" class="sidebar-link" data-v-40a3448a>9.冒泡和捕获</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_9-1-事件冒泡与事件捕获" class="sidebar-link" data-v-40a3448a>9.1 事件冒泡与事件捕获</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_9-2-addeventlistener-的第三个参数" class="sidebar-link" data-v-40a3448a>9.2 addEventListener 的第三个参数</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_9-3-事件委托" class="sidebar-link" data-v-40a3448a>9.3 事件委托</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_10-语义化" class="sidebar-link" data-v-40a3448a>10.语义化</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_10-1-语义化的特点" class="sidebar-link" data-v-40a3448a>10.1 语义化的特点</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_10-2-结构语义化" class="sidebar-link" data-v-40a3448a>10.2 结构语义化</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_10-3-ife任务" class="sidebar-link" data-v-40a3448a>10.3 IFE任务</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_11-bfc" class="sidebar-link" data-v-40a3448a>11.BFC</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_11-1-什么是bfc？" class="sidebar-link" data-v-40a3448a>11.1 什么是BFC？</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_11-2-触发条件" class="sidebar-link" data-v-40a3448a>11.2 触发条件</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_12-跨域" class="sidebar-link" data-v-40a3448a>12.跨域</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_12-1-jsonp" class="sidebar-link" data-v-40a3448a>12.1 JSONP</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_12-2-cors" class="sidebar-link" data-v-40a3448a>12.2 CORS</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_12-3-nginx反向代理" class="sidebar-link" data-v-40a3448a>12.3 nginx反向代理</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_12-4-node中间件代理" class="sidebar-link" data-v-40a3448a>12.4 node中间件代理</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_12-5-websocket" class="sidebar-link" data-v-40a3448a>12.5 websocket</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_13-es6之set和map" class="sidebar-link" data-v-40a3448a>13.ES6之set和map</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_13-2-map-字典-类对象" class="sidebar-link" data-v-40a3448a>13.2 Map (字典)[类对象]</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_14-new操作符" class="sidebar-link" data-v-40a3448a>14.new操作符</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_15-js创造对象" class="sidebar-link" data-v-40a3448a>15.js创造对象</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93.html#_16-js继承" class="sidebar-link" data-v-40a3448a>16.js继承</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-44bd5a18 data-v-44bd5a18><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-44bd5a18><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-44bd5a18></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-44bd5a18></path></svg></div></div></div>
    <script src="/assets/js/app.14e3b6f2.js" defer></script><script src="/assets/js/3.38ef4e4c.js" defer></script><script src="/assets/js/1.dd4aade8.js" defer></script><script src="/assets/js/12.b0df0a98.js" defer></script>
  </body>
</html>
